package controller;

import java.io.IOException;
import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;

import model.RoomModel;
import service.RoomService;
import service.impl.RoomServiceImpl;

@WebServlet("/servlet/CheckRoomServlet")
public class CheckRoomServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public CheckRoomServlet() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String roomType=request.getParameter("roomType");
		String startTime=request.getParameter("startTime");
		String endTime = request.getParameter("endTime");
		if(roomType==null||roomType.length()==0){
			JOptionPane.showMessageDialog(null, "请选择房间类型");
			response.sendRedirect("../RoomServlet/listType");
			return;
		}
		RoomService service=new RoomServiceImpl();
		try {
			RoomModel room=service.queryRoomByRoomType(roomType);
			int a =room.getRoomtypeqty();
			if(a>0){
				request.setAttribute("room", room);
				request.setAttribute("startTime", startTime);
				request.setAttribute("endTime", endTime);
				request.getRequestDispatcher("/Reserve.jsp").forward(request, response);
			}else {
				JOptionPane.showMessageDialog(null, "房间已经满了");
				response.sendRedirect("../RoomServlet/listType");
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
